<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\send\logic;

use app\send\model\VerificationCodeModel as VerificationCode;
/*
    验证码逻辑层
 */
class VerificationCodeLogic
{
    private $mVerificationCode;

    public function __construct(){
        $this->mVerificationCode = new VerificationCode();
    }
    /*
        保存验证码
        @param $account 手机号
        @param $code 验证码
        @param $expire_time 有效时间 
        @return false 保存失败 true 保存成功 
     */
    public function saveCode($account,$code,$expire_time){
        //查询是否有该手机号信息
        $rs = VerificationCode::where('account',$account)->find();
        //如果存在,更新.否则保存
        if($rs){
            $todayStartTime = strtotime(date("Y-m-d"));//当天0点
            //最后发送验证码时间在今天凌晨之前, 发送次数归零,否则发送次数加1
            if($rs['send_time'] <= $todayStartTime){
                $count = 1;
            }else{
                $count = $rs['count']+1;
            }
             
            $data = array(
                    'count' => $count,
                    'send_time' => time(),
                    'expire_time' => $expire_time,
                    'code' => $code,
                ); 
            $rs = $this->mVerificationCode->where('account',$account)->update($data);
        }else{
            $data = array(
                    'count' => 1,//当天已发送次数
                    'send_time' => time(),//最后发送时间
                    'expire_time' => $expire_time,//有效时间
                    'code' => $code,//验证码
                    'account' => $account,//手机号
                ); 
            $rs = $this->mVerificationCode->insert($data);
        }
        if($rs){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 手机或邮箱验证码检查，验证完后销毁验证码增加安全性,返回true验证码正确，false验证码错误
     * @param string $account 手机或邮箱
     * @param string $code 验证码
     * @param boolean $clear 是否验证后销毁验证码
     * @return string  错误消息,空字符串代码验证码正确
     */
    function cmf_check_verification_code($account, $code, $clear = false)
    {
        $findVerificationCode  = $this->mVerificationCode->where('account', $account)->find();
        
        if ($findVerificationCode) {
            if ($findVerificationCode['expire_time'] > time()) {

                if ($code == $findVerificationCode['code']) {
                    if ($clear) {
                        $this->mVerificationCode->where('account', $account)->update(['code' => '']);

                    }
                } else {
                    return "验证码不正确!";
                }
            } else {
                return "验证码已经过期,请先获取验证码!";
            }

        } else {
            return "请先获取验证码!";
        }

        return "";
    }
    /**
     * 检查手机或邮箱是否还可以发送验证码,并返回生成的验证码
     * @param string $account 手机或邮箱
     * @param integer $length 验证码位数,支持4,6,8
     * @return string 数字验证码
     */
    function cmf_get_verification_code($account, $length = 6)
    {
        if (empty($account)) return false;
        $currentTime           = time();
        $maxCount              = 20;//每天最大发送数
        $findVerificationCode  = $this->mVerificationCode->where('account', $account)->find();
        $result                = false;
        if (empty($findVerificationCode)) {
            $result = true;
        } else {
            $sendTime       = $findVerificationCode['send_time'];
            $todayStartTime = strtotime(date('Y-m-d', $currentTime));
            if ($sendTime < $todayStartTime) {
                $result = true;
            } else if ($findVerificationCode['count'] < $maxCount) {
                $result = true;
            }
        }
        
        if ($result) {
            switch ($length) {
                case 4:
                    $result = rand(1000, 9999);
                    break;
                case 6:
                    $result = rand(100000, 999999);
                    break;
                case 8:
                    $result = rand(10000000, 99999999);
                    break;
                default:
                    $result = rand(100000, 999999);
            }
        }

        return $result;
    }

}
